package com.wzh.mysending.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.wzh.mysending.R;
import com.wzh.mysending.base.BaseActivity;
import com.wzh.mysending.config.Constant;
import com.wzh.mysending.model.UserEntity;
import com.wzutils.NetUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 登录界面
 */
public class LonginActivity extends BaseActivity {

    @BindView(R.id.tietle)
    TextView tietle;
    @BindView(R.id.user_name)
    MaterialEditText userName;
    @BindView(R.id.phone_layout)
    LinearLayout phoneLayout;
    @BindView(R.id.user_pwd)
    MaterialEditText userPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;

    private AndroidNextInputs inputs;
    Intent intent;
    Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_longin);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        inputs = new AndroidNextInputs();
        inputs.add(userName, StaticScheme.NotBlank().msgOnFail("手机号码不能为空"), StaticScheme.ChineseMobile().msgOnFail("请输入正确的手机号码"));
        inputs.add(userPwd, StaticScheme.NotBlank().msgOnFail("密码不能为空"));
    }

    @OnClick(R.id.btn_login)
    public void onClick() {
        if (inputs.test()) {
            if (NetUtils.isConnected(context)) {
                goLogin();
            } else {
                toast("网络未连接……");
            }

        }
    }

    private void goLogin() {
        BmobQuery<UserEntity> eq1 = new BmobQuery<UserEntity>();
        eq1.addWhereEqualTo("userPhone", userName.getText().toString().trim());
        //--and条件2
        BmobQuery<UserEntity> eq2 = new BmobQuery<UserEntity>();
        eq2.addWhereEqualTo("userPwd", userPwd.getText().toString().trim());
        List<BmobQuery<UserEntity>> queries = new ArrayList<BmobQuery<UserEntity>>();
        queries.add(eq1);
        queries.add(eq2);

        //查询符合整个and条件的人
        BmobQuery<UserEntity> query = new BmobQuery<UserEntity>();
        query.and(queries);
        query.findObjects(new FindListener<UserEntity>() {
            @Override
            public void done(List<UserEntity> list, BmobException e) {
                if (e == null) {
                    if (list.size() == 1) {
                        UserEntity userEntity = list.get(0);
                        put(Constant.ISLOGIN, true);
                        put(Constant.ISOPEN, false);
                        put(Constant.PHONE, userEntity.getUserPhone());
                        put(Constant.USER_NAME, userEntity.getUserName());
                        put(Constant.USER_ID, userEntity.getObjectId());
                        bundle.putSerializable(Constant.USER_ENTITY, userEntity);
                        intent.setClass(context, MainActivity.class);
                        startActivity(intent);
                        finish();
                        toast("ok");
                    } else {
                        toast("账号或密码错误。");
                    }
                } else {
                    toast("查询失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });

    }


}
